<template>
    <div class="app-container">
        <el-form ref="patDischargeFormRef" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="出院方式" prop="dischargeMethod">
                <el-select v-model="form.dischargeMethod" :disabled="form.patDischargeId" placeholder="请选择出院方式">
                    <el-option
                        v-for="dict in dictDataMap['discharge_disposition_dict']"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="预出院时间" prop="patDischargeTime">
                <el-date-picker
                    v-model="form.patDischargeTime"
                    :disabled="form.patDischargeId" placeholder="请选择预出院时间"
                    style="width: 100%"
                    type="datetime"
                    value-format="YYYY-MM-DD HH:mm:ss">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="入院治疗经过" prop="treatmentProcess">
                <el-input v-model="form.treatmentProcess" :disabled="form.patDischargeId" placeholder="请输入入院治疗经过" type="textarea"/>
            </el-form-item>
            <el-form-item label="出院医嘱" prop="dischargeOrder">
                <el-input v-model="form.dischargeOrder" :disabled="form.patDischargeId" placeholder="请输入出院医嘱" type="textarea"/>
            </el-form-item>
            <el-form-item label="备注信息" prop="remarks">
                <el-input v-model="form.remarks" :disabled="form.patDischargeId" placeholder="请输入备注信息" type="textarea"/>
            </el-form-item>
            <el-button v-if="form.patDischargeId" style="float: right" type="danger" @click="handleDelete(form)">取消
            </el-button>
            <el-button v-else style="float: right" type="primary" @click="submitForm">保存</el-button>
        </el-form>
    </div>
</template>

<script lang="ts" setup>
import {
    addPatDischarge,
    delPatDischarge,
    listPatDischarge,
    stopOrdersInPatDischarge,
    updatePatDischarge
} from "@/api/patDischarge/patDischarge.ts";
import {useUserStoreHook} from "@/store/modules/user"
import {useDictStoreHook} from "@/store/modules/dict";

const dictStore = useDictStoreHook();
let dictDataMap = ref(dictStore.isTypeGetData('discharge_disposition_dict'))
const useUserStore = useUserStoreHook()
const emits = defineEmits(["saveOk"])
const {proxy} = getCurrentInstance() as any;
const props = defineProps({
    visitId: {
        type: String,
        default: ""
    },
    patientId: {
        type: String,
        default: ""
    },
    orgId: {
        type: String,
        default: ""
    }
})
const patDischargeFormRef = ref();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;
const form = ref<any>({
    dischargeMethod: "1",
    patDischargeTime: proxy.$parseTime(new Date()),
    treatmentProcess: "",
    dischargeOrder: "",
    orgId: userInfo.orgId,
    remarks: "",
    visitId: props.visitId,
    patientId: props.patientId
})
const rules = ref({
    dischargeMethod: [
        {
            required: true, message: "出院方式不能为空", trigger: "change"
        }
    ]
})

/** 提交按钮 */
function submitForm() {
    (patDischargeFormRef.value as any).validate(valid => {
        if (valid) {
            ElMessageBox.confirm('是否确认要出院',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            ).then(() => {
                if (form.value.patDischargeId != null) {
                    updatePatDischarge(form.value).then(() => {
                        ElMessage.success("出院信息修改成功");
                        emits("saveOk")
                    }).finally(() => {
                    });
                } else {
                    addPatDischarge(form.value).then((res: any) => {
                        const data = res.data;
                        if (data) {
                            const status = data.status
                            if (status == "1" || status == "2" || status == "4"
                                || status == "5" || status == "6") {
                                return ElMessage.error(data.messageStr)
                            } else if (status == "3") {
                                ElMessageBox.confirm(`${data.messageStr}`,
                                    {
                                        confirmButtonText: '确定',
                                        cancelButtonText: '取消',
                                        type: 'warning',
                                    }
                                ).then(() => {
                                    const param = {
                                        ordersIds: data.ordersIds,
                                        statusType: "9",
                                        dischargeMethod: form.value.dischargeMethod,
                                        patDischargeTime: form.value.patDischargeTime,
                                        treatmentProcess: form.value.treatmentProcess,
                                        dischargeOrder: form.value.dischargeOrder,
                                        orgId: userInfo.orgId,
                                        visitId: props.visitId,
                                        patientId: props.patientId
                                    }
                                    stopOrdersInPatDischarge(param).then(() => {
                                        ElMessage.success("出院成功")
                                        emits("saveOk")
                                    })
                                }).catch(() => {
                                })
                            } else {
                                ElMessage.success("出院成功");
                                emits("saveOk")
                            }
                        }

                    }).finally(() => {
                    });
                }
            }).catch(() => {
            })
        }
    });
}


/** 删除按钮操作 */
function handleDelete(row) {
    ElMessageBox.confirm('是否确认取消出院？',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        const patDischargeId = row.patDischargeId
        delPatDischarge(patDischargeId).then(() => {
            emits("saveOk")
            form.value.patDischargeId = null;
            ElMessage.success("删除成功");
        })
    })
}

function initOpen(visitId, patientId) {
    const param = {
        visitId: visitId,
        patientId: patientId,
        orgId: props.orgId
    }
    listPatDischarge(param).then((res: any) => {
        if (res.data && res.data.length > 0) {
            form.value = res.data[0]
        }
    })
}

defineExpose({
    initOpen
})
</script>

<style scoped>

</style>
